import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "@/store";
import FrontOrder from '../views/front/Order.vue'
Vue.use(VueRouter)


const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {

  return originalPush.call(this, location).catch(err => err)

}


const routes = [

  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue')
  },
  {
    path: '/404',
    name: '404',
    component: () => import('../views/404.vue')
  },


  {
    path: '/front',
    name: 'Front',
    component: () => import('../views/front/Front.vue'),
    redirect: '/front/home',
    meta: {
      title: "首页",
    },

    children: [
      {
        path: 'home',
        name: 'FrontHome',
        component: () => import('../views/front/Home.vue'),
        meta: {
          noNeedLogin: true
        }
      },
      {
        path: 'information',
        name: 'FrontInformation',
        component: () => import('../views/front/Information.vue'),
        meta: {
          title: "宠物资讯",
          noNeedLogin: true
        },
      },
      {
        path: 'infoDetail',
        name: 'InfoDetail',
        component: () => import('../views/front/InfoDetail.vue'),
        meta: {
          noNeedLogin: true,
          title: "资讯详情",
        },
      },
      {
        path: 'message',
        name: 'Message',
        component: () => import('../views/front/Message.vue'),
        meta: {
          noNeedLogin: true,
          title: "在线留言",
        },
      },
      {
        path: '/front/introduction',
        name: 'FrontNntroduction',
        component: () => import('../views/front/Introduction.vue'),
        meta: {
          title: "医院简介",
          noNeedLogin: true
        }
      },
      {
        path: '/front/login',
        name: 'FrontLogin',
        component: () => import('../views/front/Login.vue'),
        meta: {
          title: "登录",
          noNeedLogin: true
        }
      },
      {
        path: '/front/register',
        name: 'FrontRegister',
        component: () => import('../views/front/Register.vue'),
        meta: {
          title: "注册",
          noNeedLogin: true
        }
      },
      {
        path: 'Drug/:drugTypeId',
        name: 'FrontGood',
        component: () => import('../views/front/Drug.vue'),
        meta: {
          title: "商品页",
          noNeedLogin: true
        }
      },
      {
        path: 'detail/:drugId',
        name: 'FrontPerson',
        component: () => import('../views/front/DrugDetail.vue'),
        meta: {
          title: "商品详情页",
          noNeedLogin: true
        }
      },
      {
        path: 'shopcart',
        name: 'ShopCart',
        component: () => import('../views/front/ShopCart.vue'),
        meta: {
          title: "购物车",
        }
      },
      {
        path: 'orderDetail/:code',
        name: 'OrderDetail',
        component: () => import('../views/front/OrderDetail.vue'),
        meta: {
          title: "购物车",
        }
      },
      {//我的信息页面下的子路由页面
        path: 'my',
        name: 'FrontMy',
        // components: {
        //   aside: FrontPerson,
        //   header: FrontPerson
        // }
        redirect: '/front/my/person',
        meta: {
          title: "个人中心",
        },
        component: () => import('../views/front/My.vue'),
        children: [
          {
            path: 'person',
            name: 'FrontPerson',
            component: () => import('../views/front/Person.vue'),
            meta: {
              title: "我的信息",
            }
          },
          {
            path: 'order',
            name: 'FrontOrder',
            component: () => import('../views/front/Order.vue'),
            meta: {
              title: "我的订单",

            }
          },
          {
            path: 'mypet',
            name: 'FrontMyPet',
            component: () => import('../views/front/MyPet.vue'),
            meta: {
              title: "我的宠物",
            }
          },
          {
            path: 'address',
            name: 'FrontAddress',
            component: () => import('../views/front/Address.vue'),
            meta: {
              title: "地址管理",
            }
          },
          {
            path: 'reserve',
            name: 'FrontReserve',
            component: () => import('../views/front/Reserve.vue'),
            meta: {
              title: "可预约的医生",
            }
          },
          {
            path: 'toreserve',
            name: 'FrontToReserve',
            component: () => import('../views/front/ToReserve.vue'),
            meta: {
              title: "预约医生",
            }
          },
          {
            path: 'myreserve',
            name: 'FrontMyReserve',
            component: () => import('../views/front/MyReserve.vue'),
            meta: {
              title: "预约记录",
            }
          },
          {
            path: 'mytreatment',
            name: 'FrontMyTreatment',
            component: () => import('../views/front/MyTreatment.vue'),
            meta: {
              title: "问诊记录",
            }
          },
          {
            path: 'myauxiliarycheck',
            name: 'FrontMyAuxiliaryCheck',
            component: () => import('../views/front/MyAuxiliaryCheck.vue'),
            meta: {
              title: "辅助检查记录",
            }
          },
          {
            path: 'mydrug',
            name: 'FrontMyDrug',
            component: () => import('../views/front/MyDrug.vue'),
            meta: {
              title: "药品记录",
            }
          },
          {
            path: 'myremark',
            name: 'FrontMyRemark',
            component: () => import('../views/front/MyRemark.vue'),
            meta: {
              title: "我的留言",
            }
          },
        ]
      },
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 提供一个重置路由的方法
export const resetRouter = () => {
  router.matcher = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
  })
}

// 注意：刷新页面会导致页面路由重置
export const setRoutes = () => {
  const storeMenus = localStorage.getItem("menus");
  if (storeMenus) {

    // 获取当前的路由对象名称数组
    const currentRouteNames = router.getRoutes().map(v => v.name)
    if (!currentRouteNames.includes('Manage')) {
      // 拼装动态路由
      const manageRoute = {
        path: '/', name: '主页', component: () => import('../views/Manage.vue'), redirect: "/home", children: [
          {
            path: '/reviewtreat/:id',
            name: '复诊',
            component: () => import('../views/ReviewTreat.vue'),
          },
          {
            path: '/treateHistory/:id',
            name: '问诊记录',
            component: () => import('../views/TreatHistory.vue'),
          },
          { path: 'person', name: '个人信息', component: () => import('../views/Person.vue') },
          { path: 'password', name: '修改密码', component: () => import('../views/Password.vue') }
        ]
      }
      const menus = JSON.parse(storeMenus)
      menus.forEach(item => {
        if (item.path) {  // 当且仅当path不为空的时候才去设置路由
          let itemMenu = { path: item.path.replace("/", ""), name: item.name, component: () => import('../views/' + item.pagePath + '.vue') }
          manageRoute.children.push(itemMenu)
        } else if (item.children.length) {
          item.children.forEach(item => {
            if (item.path) {
              let itemMenu = { path: item.path.replace("/", ""), name: item.name, component: () => import('../views/' + item.pagePath + '.vue') }
              manageRoute.children.push(itemMenu)
            }
          })
        }
      })
      // 动态添加到现在的路由对象中去
      router.addRoute(manageRoute)
    }

  }
}

// 重置我就再set一次路由
setRoutes()


router.beforeEach((to, from, next) => {
  localStorage.setItem("currentPathName", to.name)  // 设置当前的路由名称
  store.commit("setPath")
  // 未找到路由的情况
  if (!to.matched.length && !to.path.includes("front")) {//后台路由菜单判断
    const storeMenus = localStorage.getItem("menus")
    if (storeMenus) {
      next("/404")
    } else {
      // 跳回登录页面
      console.log("此时调回来了")
      console.log(to.path)
      next("/login")
    }
  }
  if (to.path.includes("front")) {//前台页面判定

    if (to.path.includes("login") || to.path.includes("register")) {
      return next()
    }
    if (!to.meta.noNeedLogin) {
      if (window.localStorage.getItem('customer')) {
        next();
      } else {

        next("/front/login")
      }
    }
  }


  next()

})

export default router
